package cn.iocoder.yudao.module.enterprise.service.boundary;

import cn.iocoder.yudao.module.enterprise.controller.admin.boundary.vo.BoundaryCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.boundary.vo.BoundaryUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.boundary.BoundaryConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.boundary.BoundaryDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.boundary.BoundaryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.BOUNDARY_NOT_EXISTS;

/**
 * 组织边界 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class BoundaryServiceImpl implements BoundaryService {

    @Resource
    private BoundaryMapper boundaryMapper;

    @Override
    public Long createBoundary(BoundaryCreateReqVO createReqVO) {
        // 插入
        BoundaryDO boundary = BoundaryConvert.INSTANCE.convert(createReqVO);
        boundaryMapper.insert(boundary);
        // 返回
        return boundary.getId();
    }

    @Override
    public void updateBoundary(BoundaryUpdateReqVO updateReqVO) {
        // 校验存在
        validateBoundaryExists(updateReqVO.getId());
        // 更新
        BoundaryDO updateObj = BoundaryConvert.INSTANCE.convert(updateReqVO);
        boundaryMapper.updateById(updateObj);
    }

    @Override
    public void deleteBoundary(Long id) {
        // 校验存在
        validateBoundaryExists(id);
        // 删除
        boundaryMapper.deleteById(id);
    }

    private void validateBoundaryExists(Long id) {
        if (boundaryMapper.selectById(id) == null) {
            throw exception(BOUNDARY_NOT_EXISTS);
        }
    }

    @Override
    public BoundaryDO getBoundary(Long id) {
        return boundaryMapper.selectById(id);
    }

    @Override
    public BoundaryDO getBoundaryByPlanId(Long planId) {
        return boundaryMapper.selectByPlanId(planId);
    }

}
